package com.team.book.common.domain;

import com.team.book.common.constant.ResponseStatus;

/**
 * @author iw
 * @create 2017-05-08 15:06
 **/
public class ResponseEntity<T> {
    private int code;
    private String message;
    private T data;

    public static ResponseEntity ok(Object obj){
        return set(obj, ResponseStatus.OK);
    }
    public static ResponseEntity ok(){
        return set(null, ResponseStatus.OK);
    }

    public static ResponseEntity fail(Object obj){
        return set(obj, ResponseStatus.BAD_REQUEST);
    }
    public static ResponseEntity fail(){
        return set(null, ResponseStatus.BAD_REQUEST);
    }
    public static ResponseEntity set( ResponseStatus status){
        return set(null, status);
    }
    public static ResponseEntity set(Object obj, ResponseStatus status){
        ResponseEntity re = new ResponseEntity();
        re.setCode(status.getCode());
        re.setMessage(status.getMessage());
        if(obj!=null){
            re.setData(obj);
        }
        return re;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResponseEntity setMsg(String message) {
        this.message = message;
        return this;
    }

    public ResponseEntity setMsg(ResponseStatus status) {
        this.setCode(status.getCode());
        this.setMessage(status.getMessage());
        return this;
    }
}
